<?php
session_start(); // Initialize Session data
ob_start(); // Turn on output buffering
?>
<?php include "ewcfg7.php" ?>
<?php include "adodb" . EW_PATH_DELIMITER . "adodb.inc.php" ?>
<?php include "phpfn7.php" ?>
<?php include "provinciainfo.php" ?>
<?php include "usuariosinfo.php" ?>
<?php include "userfn7.php" ?>
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // Always modified
header("Cache-Control: private, no-store, no-cache, must-revalidate"); // HTTP/1.1 
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
?>
<?php

// Create page object
$provincia_addopt = new cprovincia_addopt();
$Page =& $provincia_addopt;

// Page init
$provincia_addopt->Page_Init();

// Page main
$provincia_addopt->Page_Main();
?>
<script type="text/javascript">
<!--
var provincia_addopt = new ew_Page("provincia_addopt");

// page properties
provincia_addopt.PageID = "addopt"; // page ID
provincia_addopt.FormID = "fprovinciaaddopt"; // form ID
var EW_PAGE_ID = provincia_addopt.PageID; // for backward compatibility

// extend page with ValidateForm function
provincia_addopt.ValidateForm = function(fobj) {
	return true; // ignore validation
}

//-->
</script>
<?php
if (EW_DEBUG_ENABLED)
	echo ew_DebugMsg();
$provincia_addopt->ShowMessage();
?>
<form name="fprovinciaaddopt" id="fprovinciaaddopt" action="provinciaaddopt.php" method="post" onsubmit="return provincia_addopt.ValidateForm(this);">
<p>
<input type="hidden" name="t" id="t" value="provincia">
<input type="hidden" name="a_addopt" id="a_addopt" value="A">
<table class="ewTableAddOpt">
	<tr>
		<td><?php echo $provincia->id->FldCaption() ?><?php echo $Language->Phrase("FieldRequiredIndicator") ?></td>
		<td><span id="el_id">
<input type="text" name="x_id" id="x_id" title="<?php echo $provincia->id->FldTitle() ?>" size="30" value="<?php echo $provincia->id->EditValue ?>"<?php echo $provincia->id->EditAttributes() ?>>
</span></td>
	</tr>
	<tr>
		<td><?php echo $provincia->Nombre->FldCaption() ?></td>
		<td><span id="el_Nombre">
<input type="text" name="x_Nombre" id="x_Nombre" title="<?php echo $provincia->Nombre->FldTitle() ?>" size="30" value="<?php echo $provincia->Nombre->EditValue ?>"<?php echo $provincia->Nombre->EditAttributes() ?>>
</span></td>
	</tr>
</table>
<p>
</form>
<?php
$provincia_addopt->Page_Terminate();
?>
<?php

//
// Page class
//
class cprovincia_addopt {

	// Page ID
	var $PageID = 'addopt';

	// Table name
	var $TableName = 'provincia';

	// Page object name
	var $PageObjName = 'provincia_addopt';

	// Page name
	function PageName() {
		return ew_CurrentPage();
	}

	// Page URL
	function PageUrl() {
		$PageUrl = ew_CurrentPage() . "?";
		global $provincia;
		if ($provincia->UseTokenInUrl) $PageUrl .= "t=" . $provincia->TableVar . "&"; // Add page token
		return $PageUrl;
	}

	// Page URLs
	var $AddUrl;
	var $EditUrl;
	var $CopyUrl;
	var $DeleteUrl;
	var $ViewUrl;
	var $ListUrl;

	// Export URLs
	var $ExportPrintUrl;
	var $ExportHtmlUrl;
	var $ExportExcelUrl;
	var $ExportWordUrl;
	var $ExportXmlUrl;
	var $ExportCsvUrl;

	// Update URLs
	var $InlineAddUrl;
	var $InlineCopyUrl;
	var $InlineEditUrl;
	var $GridAddUrl;
	var $GridEditUrl;
	var $MultiDeleteUrl;
	var $MultiUpdateUrl;

	// Message
	function getMessage() {
		return @$_SESSION[EW_SESSION_MESSAGE];
	}

	function setMessage($v) {
		if (@$_SESSION[EW_SESSION_MESSAGE] <> "") { // Append
			$_SESSION[EW_SESSION_MESSAGE] .= "<br>" . $v;
		} else {
			$_SESSION[EW_SESSION_MESSAGE] = $v;
		}
	}

	// Show message
	function ShowMessage() {
		$sMessage = $this->getMessage();
		$this->Message_Showing($sMessage);
		if ($sMessage <> "") { // Message in Session, display
			echo "<p><span class=\"ewMessage\">" . $sMessage . "</span></p>";
			$_SESSION[EW_SESSION_MESSAGE] = ""; // Clear message in Session
		}
	}

	// Validate page request
	function IsPageRequest() {
		global $objForm, $provincia;
		if ($provincia->UseTokenInUrl) {
			if ($objForm)
				return ($provincia->TableVar == $objForm->GetValue("t"));
			if (@$_GET["t"] <> "")
				return ($provincia->TableVar == $_GET["t"]);
		} else {
			return TRUE;
		}
	}

	//
	// Page class constructor
	//
	function cprovincia_addopt() {
		global $conn, $Language;

		// Language object
		$Language = new cLanguage();

		// Table object (provincia)
		$GLOBALS["provincia"] = new cprovincia();

		// Table object (usuarios)
		$GLOBALS['usuarios'] = new cusuarios();

		// Page ID
		if (!defined("EW_PAGE_ID"))
			define("EW_PAGE_ID", 'addopt', TRUE);

		// Table name (for backward compatibility)
		if (!defined("EW_TABLE_NAME"))
			define("EW_TABLE_NAME", 'provincia', TRUE);

		// Start timer
		$GLOBALS["gsTimer"] = new cTimer();

		// Open connection
		$conn = ew_Connect();
	}

	// 
	//  Page_Init
	//
	function Page_Init() {
		global $gsExport, $gsExportFile, $UserProfile, $Language, $Security, $objForm;
		global $provincia;

		// Security
		$Security = new cAdvancedSecurity();
		if (!$Security->IsLoggedIn()) $Security->AutoLogin();
		if (!$Security->IsLoggedIn()) {
			$Security->SaveLastUrl();
			$this->Page_Terminate("login.php");
		}

		// Create form object
		$objForm = new cFormObj();

		// Global Page Loading event (in userfn*.php)
		Page_Loading();

		// Page Load event
		$this->Page_Load();
	}

	//
	// Page_Terminate
	//
	function Page_Terminate($url = "") {
		global $conn;

		// Page Unload event
		$this->Page_Unload();

		// Global Page Unloaded event (in userfn*.php)
		Page_Unloaded();

		 // Close connection
		$conn->Close();

		// Go to URL if specified
		$this->Page_Redirecting($url);
		if ($url <> "") {
			if (!EW_DEBUG_ENABLED && ob_get_length())
				ob_end_clean();
			header("Location: " . $url);
		}
		exit();
	}

	//
	// Page main
	//
	function Page_Main() {
		global $objForm, $Language, $gsFormError, $provincia;

		// Process form if post back
		if ($objForm->GetValue("a_addopt") <> "") {
			$provincia->CurrentAction = $objForm->GetValue("a_addopt"); // Get form action
			$this->LoadFormValues(); // Load form values

			// Validate form
			if (!$this->ValidateForm()) {
				$provincia->CurrentAction = "I"; // Form error, reset action
				$this->setMessage($gsFormError);
			}
		} else { // Not post back
			$provincia->CurrentAction = "I"; // Display blank record
			$this->LoadDefaultValues(); // Load default values
		}

		// Perform action based on action code
		switch ($provincia->CurrentAction) {
			case "I": // Blank record, no action required
				break;
			case "A": // Add new record
				$provincia->SendEmail = TRUE; // Send email on add success
				if ($this->AddRow()) { // Add successful
					$XMLDoc = new cXMLDocument("utf-8");
					$XMLDoc->AddRoot("root");
					$XMLDoc->AddRow("result");
					$XMLDoc->AddField("x_id", strval($provincia->id->FormValue));
					$XMLDoc->AddField("x_Nombre", strval($provincia->Nombre->FormValue));
					header("Content-Type: text/xml");
					echo $XMLDoc->XML();
					$this->Page_Terminate();
					exit();
				} else {
					$this->RestoreFormValues(); // Add failed, restore form values
				}
		}

		// Render row
		$provincia->RowType = EW_ROWTYPE_ADD; // Render add type
		$this->RenderRow();
	}

	// Get upload files
	function GetUploadFiles() {
		global $objForm, $provincia;

		// Get upload data
	}

	// Load default values
	function LoadDefaultValues() {
		global $provincia;
	}

	// Load form values
	function LoadFormValues() {

		// Load from form
		global $objForm, $provincia;
		$provincia->id->setFormValue(ew_ConvertFromUtf8($objForm->GetValue("x_id")));
		$provincia->Nombre->setFormValue(ew_ConvertFromUtf8($objForm->GetValue("x_Nombre")));
	}

	// Restore form values
	function RestoreFormValues() {
		global $objForm, $provincia;
		$provincia->id->CurrentValue = ew_ConvertToUtf8($provincia->id->FormValue);
		$provincia->Nombre->CurrentValue = ew_ConvertToUtf8($provincia->Nombre->FormValue);
	}

	// Load row based on key values
	function LoadRow() {
		global $conn, $Security, $provincia;
		$sFilter = $provincia->KeyFilter();

		// Call Row Selecting event
		$provincia->Row_Selecting($sFilter);

		// Load SQL based on filter
		$provincia->CurrentFilter = $sFilter;
		$sSql = $provincia->SQL();
		$res = FALSE;
		$rs = ew_LoadRecordset($sSql);
		if ($rs && !$rs->EOF) {
			$res = TRUE;
			$this->LoadRowValues($rs); // Load row values

			// Call Row Selected event
			$provincia->Row_Selected($rs);
			$rs->Close();
		}
		return $res;
	}

	// Load row values from recordset
	function LoadRowValues(&$rs) {
		global $conn, $provincia;
		$provincia->id->setDbValue($rs->fields('id'));
		$provincia->Nombre->setDbValue($rs->fields('Nombre'));
	}

	// Render row values based on field settings
	function RenderRow() {
		global $conn, $Security, $Language, $provincia;

		// Initialize URLs
		// Call Row_Rendering event

		$provincia->Row_Rendering();

		// Common render codes for all row types
		// id

		$provincia->id->CellCssStyle = ""; $provincia->id->CellCssClass = "";
		$provincia->id->CellAttrs = array(); $provincia->id->ViewAttrs = array(); $provincia->id->EditAttrs = array();

		// Nombre
		$provincia->Nombre->CellCssStyle = ""; $provincia->Nombre->CellCssClass = "";
		$provincia->Nombre->CellAttrs = array(); $provincia->Nombre->ViewAttrs = array(); $provincia->Nombre->EditAttrs = array();
		if ($provincia->RowType == EW_ROWTYPE_VIEW) { // View row

			// id
			$provincia->id->ViewValue = $provincia->id->CurrentValue;
			$provincia->id->CssStyle = "";
			$provincia->id->CssClass = "";
			$provincia->id->ViewCustomAttributes = "";

			// Nombre
			$provincia->Nombre->ViewValue = $provincia->Nombre->CurrentValue;
			$provincia->Nombre->CssStyle = "";
			$provincia->Nombre->CssClass = "";
			$provincia->Nombre->ViewCustomAttributes = "";

			// id
			$provincia->id->HrefValue = "";
			$provincia->id->TooltipValue = "";

			// Nombre
			$provincia->Nombre->HrefValue = "";
			$provincia->Nombre->TooltipValue = "";
		} elseif ($provincia->RowType == EW_ROWTYPE_ADD) { // Add row

			// id
			$provincia->id->EditCustomAttributes = "";
			$provincia->id->EditValue = ew_HtmlEncode($provincia->id->CurrentValue);

			// Nombre
			$provincia->Nombre->EditCustomAttributes = "";
			$provincia->Nombre->EditValue = ew_HtmlEncode($provincia->Nombre->CurrentValue);
		}

		// Call Row Rendered event
		if ($provincia->RowType <> EW_ROWTYPE_AGGREGATEINIT)
			$provincia->Row_Rendered();
	}

	// Validate form
	function ValidateForm() {
		global $Language, $gsFormError, $provincia;

		// Initialize form error message
		$gsFormError = "";
		if (!is_null($provincia->id->FormValue) && $provincia->id->FormValue == "") {
			$gsFormError .= ($gsFormError <> "") ? "<br>" : "";
			$gsFormError .= $Language->Phrase("EnterRequiredField") . " - " . $provincia->id->FldCaption();
		}
		if (!ew_CheckInteger($provincia->id->FormValue)) {
			if ($gsFormError <> "") $gsFormError .= "<br>";
			$gsFormError .= $provincia->id->FldErrMsg();
		}

		// Return validate result
		$ValidateForm = ($gsFormError == "");

		// Call Form_CustomValidate event
		$sFormCustomError = "";
		$ValidateForm = $ValidateForm && $this->Form_CustomValidate($sFormCustomError);
		if ($sFormCustomError <> "") {
			$gsFormError .= ($gsFormError <> "") ? "<br>" : "";
			$gsFormError .= $sFormCustomError;
		}
		return $ValidateForm;
	}

	// Add record
	function AddRow() {
		global $conn, $Language, $Security, $provincia;

		// Check if key value entered
		if ($provincia->id->CurrentValue == "" && $provincia->id->getSessionValue() == "") {
			$this->setMessage($Language->Phrase("InvalidKeyValue"));
			return FALSE;
		}

		// Check for duplicate key
		$bCheckKey = TRUE;
		$sFilter = $provincia->KeyFilter();
		if ($bCheckKey) {
			$rsChk = $provincia->LoadRs($sFilter);
			if ($rsChk && !$rsChk->EOF) {
				$sKeyErrMsg = str_replace("%f", $sFilter, $Language->Phrase("DupKey"));
				$this->setMessage($sKeyErrMsg);
				$rsChk->Close();
				return FALSE;
			}
		}
		if ($provincia->id->CurrentValue <> "") { // Check field with unique index
			$sFilter = "(id = " . ew_AdjustSql($provincia->id->CurrentValue) . ")";
			$rsChk = $provincia->LoadRs($sFilter);
			if ($rsChk && !$rsChk->EOF) {
				$sIdxErrMsg = str_replace("%f", 'id', $Language->Phrase("DupIndex"));
				$sIdxErrMsg = str_replace("%v", $provincia->id->CurrentValue, $sIdxErrMsg);
				$this->setMessage($sIdxErrMsg);
				$rsChk->Close();
				return FALSE;
			}
		}
		$rsnew = array();

		// id
		$provincia->id->SetDbValueDef($rsnew, $provincia->id->CurrentValue, 0, FALSE);

		// Nombre
		$provincia->Nombre->SetDbValueDef($rsnew, $provincia->Nombre->CurrentValue, NULL, FALSE);

		// Call Row Inserting event
		$bInsertRow = $provincia->Row_Inserting($rsnew);
		if ($bInsertRow) {
			$conn->raiseErrorFn = 'ew_ErrorFn';
			$AddRow = $conn->Execute($provincia->InsertSQL($rsnew));
			$conn->raiseErrorFn = '';
		} else {
			if ($provincia->CancelMessage <> "") {
				$this->setMessage($provincia->CancelMessage);
				$provincia->CancelMessage = "";
			} else {
				$this->setMessage($Language->Phrase("InsertCancelled"));
			}
			$AddRow = FALSE;
		}
		if ($AddRow) {

			// Call Row Inserted event
			$provincia->Row_Inserted($rsnew);
		}
		return $AddRow;
	}

	// Page Load event
	function Page_Load() {

		//echo "Page Load";
	}

	// Page Unload event
	function Page_Unload() {

		//echo "Page Unload";
	}

	// Page Redirecting event
	function Page_Redirecting(&$url) {

		// Example:
		//$url = "your URL";

	}

	// Message Showing event
	function Message_Showing(&$msg) {

		// Example:
		//$msg = "your new message";

	}

	// Custom validate event
	// Form Custom Validate event
	function Form_CustomValidate(&$CustomError) {

		// Return error message in CustomError
		return TRUE;
	}
}
?>
